package DBTransaction;

import java.sql.SQLException;

/**
 * Created by sunwei on 2017/9/28
 * Time:17:14
 */
public class TransactionHelperTest {
    public static void main(String[] args) {
        try {
            insert1();
            insert2();
            //方法1和2都无异常，提交事务,任何一个方法出现异常都将导致事务回滚。
            TransactionHelper.commit();
        } catch (SQLException e) {
            TransactionHelper.rollback();
            throw new RuntimeException(e.getMessage(),e);
        }catch (RuntimeException e){
            TransactionHelper.rollback();
            throw new RuntimeException(e.getMessage(),e);
        }
    }
    static void insert1() throws SQLException {
        String sql = "insert into tbl_student values(23,'母大虫','123456','女','体型很胖')";

        TransactionHelper.executeNonQuery(sql);
    }

    static void insert2() throws SQLException{
        String sql = "insert into tbl_student values(24,'孙二娘','123456','女','体型很胖')";

        TransactionHelper.executeNonQuery(sql);

//        throw new RuntimeException("回滚");
    }
}
